package services

import (
	"context"
	"my-gin-tiktok/app/models"
	"my-gin-tiktok/global"
)

type userService struct {
}

var UserService = new(userService)

// GetUserByName needs to query user information by name
func (s *userService) GetUserByName(ctx context.Context, userName string) (*models.User, error) {
	var user models.User
	err := global.App.DB.WithContext(ctx).Where("name = ?", userName).First(&user).Error
	return &user, err
}

// GetUserByID needs to query user information by name
func (s *userService) GetUserByID(ctx context.Context, userId int64) (*models.User, error) {
	var user models.User
	err := global.App.DB.WithContext(ctx).Where("id = ?", userId).First(&user).Error
	return &user, err
}

// CreateUser Create a user based on the given user information, and return the user
func (s *userService) CreateUser(ctx context.Context, username string, password string) (*models.User, error) {
	user := models.User{Name: username, Password: password}
	err := global.App.DB.WithContext(ctx).Create(&user).Error
	return &user, err
}
